/*!
 * Ext JS Library 3.1.1
 * Copyright(c) 2006-2010 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
HouseGrid = function(viewer, config) {
    this.viewer = viewer;
    Ext.apply(this, config);

    this.store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/desktop/get_house'
        }),

				reader: new Ext.data.JsonReader({
					totalProperty: 'results', 
					root: 'rows',							
					fields: [
						{name: 'id'},
	 					{name: 'fybh', 	type: 'string'},             
	 					{name: 'jy', 		type: 'string'},             
	 					{name: 'cq', 		type: 'string'},                     
	 					{name: 'lpzd', 	type: 'string'},             
	 					{name: 'lzwz', 	type: 'string'},             
	 					{name: 'fh', 		type: 'string'},             
	 					{name: 'lc',	 	type: 'string'},             
	 					{name: 'fx', 		type: 'string'},             
	 					{name: 'mj',	 	type: 'string'},             
	 					{name: 'cx',	 	type: 'string'},             
	 					{name: 'szj',		type: 'string'},             
	 					{name: 'zzj',		type: 'string'},	           
	 					{name: 'bm',		type: 'string'},             
	 					{name: 'yg',		type: 'string'},	           
	 					{name: 'gs',		type: 'string'},             
	 					{name: 'create_at', type: 'date', dateFormat: 'Y-m-d H:i:s'},
	 					{name: 'update_at', type: 'date', dateFormat: 'Y-m-d H:i:s'}          
 					]		 
				}),
				sortInfo:{field: 'id', direction: "ASC"}
    });
		
		
		this.store.load();
		
    this.store.setDefaultSort('id', "DESC");


    this.columns = [
			{ id: 'id', header: "id", dataIndex: 'id', hidden: true, width: 25,sortable: true},
			{ header: "房源编号",   dataIndex: 'fybh',  hidden: true,	width: 100, sortable:true},
			{ header: "交易",   	dataIndex: 'jy', 		width: 100, sortable: true},
			{ header: "城区",   	dataIndex: 'cq', 		width: 100, sortable: true},
			{ header: "楼盘字典", 	dataIndex: 'lpzd', 	width: 100, sortable: true},
			{ header: "栋座位置", 	dataIndex: 'lzwz', 	width: 100, sortable: true},
			{ header: "房号",     dataIndex: 'fh',  	width: 100, sortable:true},
			{ header: "楼层",  		dataIndex: 'lc',  	width: 100, sortable:true},
			{ header: "房型",  		dataIndex: 'fx',  	width: 100, sortable:true},
			{ header: "面积",   	dataIndex: 'mj',  	width: 100, sortable:true},
			{ header: "朝向",   	dataIndex: 'cx',  	width: 100, sortable:true},
			{ header: "售总价",   	dataIndex: 'szj',  	width: 100, sortable:true},
			{ header: "租总价",   	dataIndex: 'zzj',  	width: 100, sortable:true},
			{ header: "委托日",   	dataIndex: 'create_at',  	width: 100, sortable:true},
			{ header: "部门",   	dataIndex: 'bm',  	width: 100, sortable:true},
			{ header: "员工",   	dataIndex: 'yg',  	width: 100, sortable:true},
			{ header: "公私",   	dataIndex: 'gs',  	width: 100, sortable:true},
			{ header: "最后跟进日",  dataIndex: 'update_at',  	width: 100, sortable:true}
		];

    HouseGrid.superclass.constructor.call(this, {
        region: 'center',
        id: 'house-grid',
        loadMask: {msg:'正在载入...'},

        sm: new Ext.grid.RowSelectionModel({
            singleSelect:true
        }),

        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            showPreview:true,
            getRowClass : this.applyRowClass
        }
    });

    this.on('rowcontextmenu', this.onContextClick, this);
};

Ext.extend(HouseGrid, Ext.grid.EditorGridPanel, {

    onContextClick : function(grid, index, e){
        if(!this.menu){ // create context menu on first right click
            this.menu = new Ext.menu.Menu({
                id:'task-ctx',
                items: [{
                    text: '巡查点管理',
                    iconCls: 'new-tab',
                    scope:this,
                    handler: function(){
                      //this.viewer.openTab(this.ctxRecord);

												var main_tab = Ext.getCmp("main-tabs");
												var id = 'inspect-view';
								        var tab;
								        if(!(tab = main_tab.getItem(id))){
								            tab = new InspectPanel;
								            main_tab.add(tab);
								        }
												tab.grid.loadFeed("inspects", this.ctxRecord.id);
												Ext.getCmp('inspect-view').setTitle("巡查点管理"+this.ctxRecord.id);
								        main_tab.setActiveTab(tab);				
                    }
                },{
                    iconCls: 'new-win',
                    text: '查看地图',
                    scope:this,
                    handler: function(){
                        //window.open(this.ctxRecord.data.link);
                    }
                },'-',{
                    iconCls: 'refresh-icon',
                    text:'刷新',
                    scope:this,
                    handler: function(){
	                    this.ctxRow = null;
                      this.store.reload();
                    }
                },{
                  iconCls: 'refresh-icon',
                  text:'删除记录',
                  scope:this,
                  handler: function(){
											var pars = "id=" + this.ctxRecord.id;
											new Ajax.Request("/feed/delete_plan", { 
												method: "POST",
													parameters: pars,
													onComplete:	 function(request) {
				                    this.ctxRow = null;
			                      this.store.reload();
													}
											});
									}		
								}]
            });
            this.menu.on('hide', this.onContextHide, this);
        }
        e.stopEvent();
        if(this.ctxRow){
            Ext.fly(this.ctxRow).removeClass('x-node-ctx');
            this.ctxRow = null;
        }
        this.ctxRow = this.view.getRow(index);
        this.ctxRecord = this.store.getAt(index);
        Ext.fly(this.ctxRow).addClass('x-node-ctx');
        this.menu.showAt(e.getXY());
    },

    onContextHide : function(){
        if(this.ctxRow){
            Ext.fly(this.ctxRow).removeClass('x-node-ctx');
            this.ctxRow = null;
        }
    },

    loadFeed : function(url) {
        this.store.baseParams = {
            feed: url
        };
        this.store.load();
    },

    formatDate : function(date) {
        if (!date) {
            return '';
        }
        var now = new Date();
        var d = now.clearTime(true);
        var notime = date.clearTime(true).getTime();
        if (notime == d.getTime()) {
            return 'Today ' + date.dateFormat('g:i a');
        }
        d = d.add('d', -6);
        if (d.getTime() <= notime) {
            return date.dateFormat('D g:i a');
        }
        return date.dateFormat('n/j g:i a');
    },

    formatTitle: function(value, p, record) {
        return String.format(
                '<div class="topic"><b>{0}</b><span class="author">{1}</span></div>',
                value, record.data.author, record.id, record.data.forumid
                );
    }
});

Ext.reg('housegrid', HouseGrid);